﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Eshouse.Bll;
using Eshouse.Entity;
using System.Web.Security;

namespace Eshouse.Web.Controllers
{
    public class UserCenterController : Controller
    {
        #region  用户注册
        // GET: /UserCenter/
        [HttpPost]
        public ActionResult GetImgCode()
        {
            string res;
            res = "0";
            string imgcode = publicComm.GetCode;
            Session["imgcode"] = imgcode;
            res = Request.ApplicationPath + publicComm.GetImge(imgcode);
            return Content(res);
        }
        [HttpPost]
        public ActionResult VerifyUserName()
        {
            string res = "1";
            string userName = Request["username"].ToString();
            if (userName == "")
                res = "0";
            if(personAccess.VerifyUserName(userName))
                res="0";
            return Content(res);
        }
        [HttpPost]
        public ActionResult InsertUser()
        {
            string seccess = "1";
            string username = Request["username"].ToString();
            string userpwd = Request["userpwd"].ToString();
            string imgCode = Request["imgcode"].ToString();
            if(Session["imgcode"]==null)
                return Content("验证码不正确！");
            string code = Session["imgcode"].ToString();
            if(code.ToLower()!=imgCode.ToLower())
                return Content("验证码不正确！");
            if(personAccess.VerifyUserName(username))
                 return Content("该用户名已经存在!");
            PersonInfo p = new PersonInfo();
            CRC32Primarykey crc=new CRC32Primarykey();
            p.Id = crc.createIntID(DateTime.Now.ToString() + username);
            p.PSLoginTime = DateTime.Now;
            p.PSRegTime = DateTime.Now;
            p.PSUserName = username;
            p.PSPassword = publicComm.toMD5(userpwd);
            SysRole role = new SysRole();
            role.Id = -10000000; //默认一般用户 
            p.Role = role;
            string ip = Request.UserHostAddress;
            personAccess pas = new personAccess();
            if (!pas.InsertNewUser(p,ip))
            {
                seccess = "对不起，发生未知错误，注册失败，请您稍后再试!";
            }
            else { ///用户注册 成功 并设置认证通过

                FormsAuthentication.SetAuthCookie(username,false);
            }
            return Content(seccess);           
        }
        public ActionResult Register()
        {
            string imgcode = publicComm.GetCode;
            Session["imgcode"] =imgcode;
            ViewData["imgcode"] =Request.ApplicationPath+ publicComm.GetImge(imgcode);
            return View();
        }
        public ActionResult Login()
        {
            return View();
        }
        #endregion
        #region  完成用户详情
        [Authorize]
        public ActionResult UserDetail()
        {
            return View();
        }
        /// <summary>
        /// 上传照片
        /// </summary>
        /// <param name="FileData"></param>
        /// <param name="folder"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ImportImg(HttpPostedFileBase FileData, string folder, string name)
        {

            string result = "";
            if (null != FileData)
            {
                try
                {
                    result = Path.GetFileName(FileData.FileName);//获得文件名
                    string ext = Path.GetExtension(FileData.FileName);//获得文件扩展名
                    CRC32Primarykey crc = new CRC32Primarykey();
                    DateTime now=DateTime.Now;
                    string fileName = crc.createIntID(now.ToString()+ now.Millisecond.ToString()+"user").ToString();
                    string saveName = fileName + ext;//实际保存文件名
                    result = fileName;
                    saveFile(FileData, folder, saveName);//保存文件
                }
                catch
                {
                    result = "";
                }
            }
            return Content(result);
        }
        [NonAction]
        private void saveFile(HttpPostedFileBase postedFile, string filepath, string saveName)
        {
            string phyPath = Request.MapPath("~" + filepath + "/");
            if (!Directory.Exists(phyPath))
            {
                Directory.CreateDirectory(phyPath);
            }
            try
            {
                postedFile.SaveAs(phyPath + saveName);
            }
            catch (Exception e)
            {
                throw new ApplicationException(e.Message);

            }
        }
        #endregion
    }
}
